<include file="__THEME__/public_header" />
<link rel="stylesheet" href="__THEME__/js/swfupload/css/style.css">
<script type="text/javascript" src = "__THEME__/js/swfupload/js/fileprogress.js"></script>
<script type="text/javascript" src = "__THEME__/js/swfupload/js/handlers.js"></script>
<script type="text/javascript" src = "__THEME__/js/swfupload/swfupload/swfupload.js"></script>
<script type="text/javascript" src = "__THEME__/js/swfupload/js/swfupload.queue.js"></script>
<script type="text/javascript">
var swfu;
window.onload = function() {
  var settings = {
    flash_url : "__THEME__/js/swfupload/swfupload/swfupload.swf",
    upload_url: "{:U('admin/Resource/doAddAttach')}", 
    post_params: {"PHPSESSID": "<?php echo session_id(); ?>"},
    file_size_limit : "1024 MB",
    file_post_name:'file',
    file_types : "*.flv;*.f4v;*.mp4;*.mp3;",
    file_types_description : "All Files",
    file_upload_limit : 1,  //配置上传个数
    file_queue_limit : 0,
    custom_settings : {
      progressTarget : "fsUploadProgress",
      cancelButtonId : "btnCancel"
    },
    debug: false,
    // Button settings
    button_image_url: "__THEME__/js/swfupload/TestImageNoText_65x29.png",
    button_width: "100",
    button_height: "29",
    button_top_padding: 30,
    button_placeholder_id: "spanButtonPlaceHolder",
    button_text_style: ".theFont { font-size: 16; }",
    button_text_left_padding: 12,
    button_text_top_padding: 3, 
    file_queued_handler : fileQueued,
    file_queue_error_handler : fileQueueError,
    file_dialog_complete_handler : fileDialogComplete,
    upload_start_handler : uploadStart,
    upload_progress_handler : uploadProgress,
    upload_error_handler : uploadError,
    upload_success_handler : uploadSuccess,
    upload_complete_handler : uploadComplete,
    queue_complete_handler : queueComplete  
  };
  swfu = new SWFUpload(settings);
};
</script>
<div class="content">
	<div class="wrap">
    	<div class="con_wrap">
        	 <div class="con_tit">
                <span>资源<if condition="$data">修改<else/>上传</if></span>
            </div>
            <div class="con_box">
                <div class="slect_item">
                    <ul class="slect_list">
                        <li>
                            <div class="txt">资源来源：</div>
                            <div class="inp">
                                <input type="radio" name="type" value="1" <if condition="$data"><if condition="$data['type'] eq 1">checked="checked"</if><else/>checked="checked"</if> onclick="selectType(1);">培训资讯&nbsp;&nbsp;
                                <input type="radio" name="type" value="2" <if condition="$data['type'] eq 2">checked="checked"</if> onclick="selectType(2);">学习资源
                            </div>
                        </li>
                    </ul>
                    <if condition="$data">
                        <ul class="slect_list">
                            <li>
                                <div class="txt">旧分类：</div>
                                <div class="inp" style="width:1000px;margin-left:100px;margin-top:-30px;">
                                  {$data.type_name}
                                </div>
                            </li>
                        </ul>
                    </if>
                    <ul class="slect_list" style="width:1000px;">
                        <li>
                            <div class="txt" id="type_list">选择分类：</div>
                            <div class="inp" style="width:1000px;margin-left:100px;margin-top:-30px;">
                               <select id="type1" style="margin-right:15px;" onchange="select_type(1)">
                                   <option value="0,0">---请选择---</option>
                                   <volist name="resource_type" id="resource_type">
                                       <option value="{$resource_type.id},{$resource_type.type}">{$resource_type.name}</option>
                                   </volist>
                               </select>
                            </div>
                        </li>
                    </ul>
                	<ul class="slect_list">
                        <li>
                            <div class="txt">资源类型：</div>
                            <div class="inp">
                                <input type="radio" name="category" value="1" <if condition="$data"><if condition="$data['category'] eq 1">checked="checked"</if><else/>checked="checked"</if> >文档资源&nbsp;&nbsp;
                                <input type="radio" name="category" <if condition="$data['category'] eq 2">checked="checked"</if> value="2">视频资源
                            </div>
                        </li>
                    </ul>
                    <ul class="slect_list">
                        <li>
                            <div class="txt">资源名称：</div>
                            <div class="inp">
                                <input type="text" id="resource_name" value="{$data.resource_name}" style="height: 30px;">
                            </div>
                        </li>
                    </ul>
                    <ul class="slect_list">
                        <li>
                            <div class="txt">资源图片：</div>
                            <div class="inp" style="width:400px;height:200px;margin: -40px 0px 0px 90px;" id="msg">
                                <img src="<if condition='$data'>{$data.img_url}<else/>__ROOT__/apps/public/_static/image/hdfm.png</if>" id="cover_yulan" style="width:370px;height:210px;"/>
                            </div>&nbsp;&nbsp;
                            <form name="uploadFrom" id="uploadFrom" action="{:U('admin/Resource/doAddImg')}" method="post"  target="tarframe" enctype="multipart/form-data">
                            <input type="file" style="margin: -190px 0px 180px 465px;" name="cover" id="upload_file" onchange="handleFiles(this)">
                            </form>
                            <iframe src=""  width="0" height="0" style="display:none;" name="tarframe"></iframe>
                            <input name="old_cover" type="hidden" value='{$data.resource_img}'>
                        </li>
                    </ul>
                    <if condition="$data">
                        <ul class="slect_list">
                            <li>
                                <div class="txt">资源视频：</div>
                                <div class="inp" style="width:400px;height:200px;margin: -40px 0px 0px 90px;"> 
                                    <video width="100%" height="100%" controls="controls">
                                      <source src="{$data['file_url']}" type="video/ogg">
                                      <source src="{$data['file_url']}" type="video/mp4">
                                    </video>
                                </div>
                            </li>
                        </ul>
                    </if>
                    <ul class="slect_list">
                        <li>
                            <div class="txt">上传资源：</div>
                            <div class="inp" style="width:600px;height: 100px;padding-left:90px;margin-top:-20px;">
                                <div class="swfupload-box">
                                    <div class="fieldset flash" id="fsUploadProgress" ></div><br/>
                                    <div>
                                      <div>
                                        <span id="spanButtonPlaceHolder"></span>
                                      </div>
                                      <div>
                                      <input id="btnCancel" type="button" value="取消上传" onclick="testclick();"  style="margin-left: 5px; font-size: 8pt; height: 29px; width:70px;background-color:#f0f0f0;color:#535353;" />
                                      </div>
                                    </div>
                                </div>
                            </div>
                        </li>
                    </ul>
                    <ul class="slect_list">
                        <li>
                            <div class="txt">资源介绍：</div>
                            <div class="inp" style="height: 200px;">
                              <textarea id="resource_info" style="width:500px;height: 200px;padding-left: -100px;">{$data.resource_info}</textarea>
                            </div>
                        </li>
                    </ul>
                    <ul class="slect_list">
                        <li>
                            <div class="txt">是否推荐：</div>
                            <div class="inp">
                                <input type="radio" name="is_tj" value="1" <if condition="$data['is_tj'] eq 1">checked="checked"</if>>推荐&nbsp;&nbsp;
                                <input type="radio" name="is_tj" value="2" <if condition="$data"><if condition="$data['is_tj'] eq 2">checked="checked"</if><else/>checked="checked"</if> >不推荐
                            </div>
                        </li>
                    </ul>
                </div>
                <div class="slect_item mt30">
                	<ul class="slect_list">
                        <li>
                            <div class="txt"></div>
                            <div class="inp">
                                <input type="hidden" id="attach_id" name="attach_id" value="{$data.attach_id}">
                                <input type="hidden" id="id" value="{$data.id}">
                                <input type="hidden" id="resource_img" value="{$data.resource_img}">
                                <input type="hidden" id="resource_type" value="{$data.resource_type}">
                            	<a class="save" href="javascript:addResource();">确定</a>
                                <a href="javascript:;">取消</a>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>
<input type="hidden" id="type_count">
<input type="hidden" id="resource_teacher">
<style>
.progressWrapper{width:400px;}
</style>
<script>
    $(document).ready(function(){  
        var old_type="<?php echo $data['type']?>";
        if(old_type==2){
            selectType(2);
        }
    }); 
    window.URL = window.URL || window.webkitURL;
    var fileElem = document.getElementById("cover");
    function stopSend(url,id){
     if(url){
        $('#cover_yulan').attr('src',url);
        $("#resource_img").val(id);
     }else{
      alert("上传失败");
     }
    }
    function handleFiles(obj) {
      $("#uploadFrom").submit();
    }
    function addResource(){
        var type=$('input[name="type"]:checked ').val();
        var id=$("#id").val();
        if(id==""){
            id=0;
        }
        var type1=$("#type1").val().split(",");
        if(type1[0]<1){
            if($("#resource_type").val()){
                resource_type=$("#resource_type").val();
            }else{
                alert("请选择分类");
                return; 
            } 
        }
        if(!resource_type){
            if(type==1){
                var type2=$("#type1").next('select').val().split(",");
                if(type2[0]<1){
                    alert("请选择子类");
                    return;
                }
                resource_type=type1[0]+"/"+type2[0];
            }else{
                var resource_type="";
                var type_count=$("#type_count").val().split(",");
                for (var i = 0; i < type_count.length; i++) {
                    var type2="";
                    type2=$("#type"+type_count[i]).val().split(",");
                    var id_1=type2[0];
                    if(id_1<1){
                        alert("请选择子类");
                        return;
                    }
                    if(type_count[i]==31 || type_count[i]==46){
                        $("#resource_teacher").val(id_1);
                    }else{
                        resource_type+=type1[0]+"/"+type_count[i]+"/"+id_1+",";
                    }
                }
                resource_type=resource_type.substring(0,resource_type.length-1);
            }
        }
        var resource_teacher=$("#resource_teacher").val();
        var is_tj=$('input[name="is_tj"]:checked ').val(); 
        var category=$('input[name="category"]:checked ').val(); 
        var resource_name=$("#resource_name").val();
        var attach_id=$("#attach_id").val();
        var resource_info=$("#resource_info").val();
        var resource_img=$("#resource_img").val();
        if(resource_name==""){
            alert("资源名称不能为空");
            return;
        }
        if(resource_img==""){
            alert("请上传封面");
            return;
        }
        if(attach_id==""){
            alert("请上传视频");
            return;
        }
        if(resource_info==""){
            alert("资源介绍不能为空");
            return;
        }
        $.ajax({
            type: "POST",
            url:"{:U('admin/Resource/doAddResource')}",
            data:"category="+category+"&resource_name="+resource_name+"&resource_type="+resource_type+"&attach_id="+attach_id+"&resource_img="+resource_img+"&resource_info="+resource_info+"&is_tj="+is_tj+"&type="+type+"&resource_teacher="+resource_teacher+"&id="+id,
            dataType:"text",
            success:function(data){
             if(isNaN(data)){
                  alert(data);
              }else{
                window.location.href="{:U('admin/Resource/admin_show_resource')}";
              }
            }
        });
    }
    function selectType(type){
        $.ajax({
          type: "GET",
          url:"{:U('admin/Resource/getResourceType')}",
          data:"parent=0&type="+type,
          dataType:"json",
          success:function(data){
            $("#type_list").nextAll().remove();
            var str="";
            str="<div class='inp' id='type_select' style='width:1000px;margin-left:100px;margin-top:-30px;'>";
            str+="<select id='type1' style='margin-right:15px;' onchange='select_type(1)'>";
            str+="<option value='0,0'>---请选择---</option>";
            $.each(data,function(i,item){     
                str+="<option value='"+item.id+","+item.type+"'>"+item.name+"</option>";
            });
            str+="</select>";
            str+="</div>";
            $("#type_list").after(str); 
          }
        });
    }
    function select_type(id){
        var str=$("#type"+id).val().split(",");
        var parent=str[0];
        var type=str[1];
        $.ajax({
          type: "GET",
          url:"{:U('admin/Resource/getResourceType')}",
          data:"parent="+parent+"&type="+type,
          dataType:"json",
          success:function(data){
            $("#type"+id).nextAll().remove();
            if(parent==1){
                var str="";
                str+="<select style='margin-right:15px;'>";
                str+="<option value='0,0'>---请选择---</option>";
                $.each(data,function(i,item){
                    str+="<option value='"+item.id+","+item.type+"'>"+item.name+"</option>";
                }); 
                str+="</select>";
                $("#type"+id).after(str);
            }else{
                var type_count="";
                $.each(data,function(i,item){
                    if(i*1+1==data.length){
                        type_count+=item.id;
                    }else{
                        type_count+=item.id+",";
                    }
                    var str="";
                    str+="<div>"+item.name+"：</div>";
                    str+="<select style='margin-right:15px;' id='type"+item.id+"'>";
                    str+="<option value='0,0'>---请选择---</option>";
                    $.each(item.level,function(i1,item1){
                        str+="<option value='"+item1.id+","+item1.type+"'>"+item1.name+"</option>";
                    }); 
                    str+="</select>";
                    $("#type_select").append(str);
                });
                $("#type_count").val(type_count);   
            } 
          }
        });
    }
</script>
<include file="__THEME__/public_footer" />